/*
 * tarm_communication.c
 *
 *  Created on: 2019��11��8��
 *      Author: AN
 */

#include "tarm_uart.h"

#include "tarm_communication.h"
#include "tarm_limit.h"
#include "tarm_pwm.h"
#include "tarm_baseTimer.h"

#include "application/tarm_run.h"
#include "application/tarm_init.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>


unsigned int Esp8266_InitFlag=0;	//

/************************************************************
*	不断串口输出提醒输入命令；自动返回接收到的命令
*	传入参数：	无
*	返回值：	无
*	执行位置：	在main.c的
************************************************************/

void uart_tips(void)
{
	uint16_t times=0;
	//char report_str[20]="0";

	if(StructUart0RxBuf.FinishFlag==2)
	{
		//1-reshow the rec comment
		PRINTF_UART0("<uart0 rec:\n");
		PRINTF_UART0(StructUart0RxBuf.Rxbuf);
		PRINTF_UART0("<uart0 rec end>\r\n");

		//2-operate
		command_operate((uint8_t*)StructUart0RxBuf.Rxbuf, StructUart0RxBuf.RxbufSize);

		//3-clear the flag
		memset(StructUart0RxBuf.Rxbuf, 0, sizeof(StructUart0RxBuf.Rxbuf)/sizeof(char));
		StructUart0RxBuf.FinishFlag = 0;
		StructUart0RxBuf.RxbufSize = 0;

		//4-
	}
	else
	{
		times++;
		if(times%5000==0)
		{
			PRINTF_UART0("\r\n CNC_ARM \r\n");
			PRINTF_UART0("CNC_ARM@day_day\r\n\r\n");
		}
		if(times%200==0)PRINTF_UART0("请输入数据,以回车键结束\n\nplease enter the command, end with enter");
		delay_ms(10);
	}
}

void uart2_tips(void)
{
	uint16_t times=0;
	//char report_str[20]="0";

	if(StructUart1RxBuf.FinishFlag==2)
	{
		//1-reshow the rec comment
		PRINTF_UART0("<uart1 rec:\n");
		PRINTF_UART0(StructUart1RxBuf.Rxbuf);
		PRINTF_UART0("<uart1 rec end>\r\n");

		//2-operate
		command_operate((uint8_t*)StructUart1RxBuf.Rxbuf, StructUart1RxBuf.RxbufSize);

		//3-clear the flag
		memset(StructUart1RxBuf.Rxbuf, 0, sizeof(StructUart1RxBuf.Rxbuf)/sizeof(char));
		StructUart1RxBuf.FinishFlag = 0;
		StructUart1RxBuf.RxbufSize = 0;

		//4-
	}
	else
	{
		times++;
		if(times%5000==0)
		{
//			PRINTF_UART1("\r\n UART1 \r\n");
//			PRINTF_UART1("CNC_ARM@day_day\r\n\r\n");
		}
//		if(times%200==0)
//			PRINTF_UART1("����������,�Իس�������\n");
		delay_ms(10);
	}
}

int esp8266_init(char *esp_char)
{
//	//int esp_char_len;
//
//	uint8_t AT_String[3][25] = {"AT\r\r\n","AT+CIPMUX=1\r\r\n","AT+CIPSERVER=1,6000\r\r\n"};
//	int len1 = sizeof(AT_String[0]) / sizeof(AT_String[0][0]);
//	int len2 = sizeof(AT_String[1]) / sizeof(AT_String[1][0]);
//	int len3 = sizeof(AT_String[2]) / sizeof(AT_String[2][0]);
//	int t;
//	delay_ms(300);
////	for(t=0;t<len1;t++)
////	{
////		usart_data_transmit(USART0, AT_String[0][t]);
////		while(usart_flag_get(USART0,USART_FLAG_TC)!=SET);//�ȴ����ͽ���
////	}
////	delay_ms(300);
////	for(t=0;t<len2;t++)
////	{
////		usart_data_transmit(USART0, AT_String[1][t]);
////		while(usart_flag_get(USART0,USART_FLAG_TC)!=SET);//�ȴ����ͽ���
////	}
////	printf("\nAT1 write\n");
////	delay_ms(300);
////	for(t=0;t<len3;t++)
////	{
////		usart_data_transmit(USART0, AT_String[2][t]);
////		while(usart_flag_get(USART0,USART_FLAG_TC)!=SET);//�ȴ����ͽ���
////	}
////	printf("\nAT2 write\n");
//
//	/*while(!(USART2_RX_STA&0x8000))
//	{
//	}
//
//	esp_char_len=USART2_RX_STA&0x3fff;	//�õ��˴ν��յ������ݳ���
//	for(t=0;t<esp_char_len;t++)
//	{
//		esp_char[t] = USART2_RX_BUF[t];
//	}
//
//	if(esp_char[t-2] == 'K'){
//		Esp8266_InitFlag = 1;
//		return 1;
//	}
//	else {
//		Esp8266_InitFlag = 0;
//		return 0;
//	}*/
	return 1;
}

int esp8266_report(void)
{
	char AT_String[25];
	char Report_String[100];
	int len_AT_String,len_Report_String;

	if(Esp8266_InitFlag != 1)
	{
		return -1;
	}

	sprintf(Report_String,"_&x:%f;\t_&y:%f;\n_&prime:%f;\t_&vice:%f;\r\r\n",
			g_TARM_RunRecord.pv_current.x,
			g_TARM_RunRecord.pv_current.y,
			g_TARM_RunRecord.pv_current.p,
			g_TARM_RunRecord.pv_current.v);
	len_Report_String = sizeof(Report_String) / sizeof(Report_String[0]);

	sprintf(AT_String,"AT+CIPSEND=0,%d\r\r\n",len_Report_String);
	len_AT_String = sizeof(AT_String) / sizeof(AT_String[0]);

	PRINTF_UART1OnSize(AT_String, len_AT_String);

	delay_ms(300);

	PRINTF_UART1OnSize(Report_String, len_Report_String);

	return 1;
}


int esp8266_report_command(char *str,int len)
{
	char AT_String[25];
	int len_AT_String;

	sprintf(AT_String,"AT+CIPSEND=0,%d\r\r\n",len);
	len_AT_String = sizeof(AT_String) / sizeof(AT_String[0]);

	PRINTF_UART1OnSize(AT_String, len_AT_String);
	delay_ms(300);

	PRINTF_UART1OnSize(str, len);

	return 1;
}

int console_report(void)
{
	char AT_String[25];
	char Report_String[100];
	int len_AT_String,len_Report_String;

	if(Esp8266_InitFlag != 1)
	{
		return -1;
	}

	sprintf(Report_String,"_&x:%f;\t_&y:%f;\n_&prime:%f;\t_&vice:%f;\r\r\n",
			g_TARM_RunRecord.pv_current.x,
			g_TARM_RunRecord.pv_current.y,
			g_TARM_RunRecord.pv_current.p,
			g_TARM_RunRecord.pv_current.v);
	len_Report_String = sizeof(Report_String) / sizeof(Report_String[0]);

	sprintf(AT_String,"AT+CIPSEND=0,%d\r\r\n",len_Report_String);
	len_AT_String = sizeof(AT_String) / sizeof(AT_String[0]);

	PRINTF_UART0OnSize(AT_String, len_AT_String);

	delay_ms(300);

	PRINTF_UART0OnSize(Report_String, len_Report_String);

	return 1;
}

int console_report_command(char *str,int len)
{
	PRINTF_UART0OnSize(str, len);
	return 1;
}

